/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Util;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import pe.edu.pucp.Envios.DAO.EnviosDAO;
import pe.edu.pucp.Modelo.Ciudad;
import pe.edu.pucp.Modelo.Envio;
import pe.edu.pucp.Modelo.Pago;
import pe.edu.pucp.Modelo.Usuario;
import pe.edu.pucp.Ventas.DAO.PagoDAO;
import pe.edu.pucp.sigepa.recocidosimulado.domain.interfaz.Solucion;


/**
 *
 * @author katy
 */
public class PdfHelper {
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    
   public static void crearPDFDocumentoPago(String direccionDelDocumento,Envio envio, Pago p, Usuario user, Ciudad destino, Solucion lstVuelos, double igv) throws Exception {
        Document document = new Document();
        
        PdfWriter writer  = PdfWriter.getInstance(document, new FileOutputStream(direccionDelDocumento));
        document.open();
        document.addTitle("Documento de pago N°: " + p.getIdpago() + " - ENVIOS SIGEPA");
        document.addSubject("Using iText");

        document.addKeywords("Java, PDF, iText, NetBeans");
        document.addAuthor(user.getAppaterno() + " " + user.getApmaterno() + " " + user.getNombre());

        llenarPDFDocumentoPago(document,envio,p, user,writer, destino, lstVuelos, igv);
        document.close();
    }
   
   public static void llenarPDFDocumentoPago(Document d, Envio e,Pago p, Usuario user, PdfWriter writer, Ciudad destino, Solucion lstVuelos, double igv) throws DocumentException {
   
        SimpleDateFormat formato = new SimpleDateFormat("hh:mm:ss aaa");
        Paragraph ParrafoHoja = new Paragraph();
        EnviosDAO env = new EnviosDAO();
        PagoDAO dao = new PagoDAO();        
        agregarLineasEnBlanco(ParrafoHoja, 1);        
        ParrafoHoja.add(new Paragraph("", catFont) );               
        agregarLineasEnBlanco(ParrafoHoja, 1);
        Paragraph parrafo = new Paragraph("COMPROBANTE DE PAGO N°: DP000" + p.getIdpago() + " - Envios SIGEPA", catFont);
        parrafo.setAlignment(Element.ALIGN_CENTER);
        ParrafoHoja.add(parrafo);
        agregarLineasEnBlanco(ParrafoHoja, 1);        
        PdfPTable table4 = new PdfPTable(1); 
        float[] columnWidths4 = {8f};
        table4.setWidths(columnWidths4);
        PdfPCell c1 = new PdfPCell(new Paragraph("Empleado: " + user.getAppaterno() + " " + user.getAppaterno() +", " + user.getNombre(), smallBold));        
        c1.setBorder(Rectangle.NO_BORDER);
        table4.addCell(c1);
        PdfPCell c2 = new PdfPCell(new Paragraph("Documento: " + user.getDocumento(), smallBold));
        c2.setBorder(Rectangle.NO_BORDER);        
        table4.addCell(c2);
        parrafo.add(table4);
        agregarLineasEnBlanco(ParrafoHoja, 1);
        parrafo = new Paragraph("");
        parrafo.setAlignment(Element.ALIGN_LEFT);
        ParrafoHoja.add(parrafo);
        PdfPTable table3 = new PdfPTable(2); // 4 columns.
        float[] columnWidths3 = {4f, 4f};
        table3.setWidths(columnWidths3);
        
        PdfPCell cel1 = new PdfPCell(new Paragraph("Cliente: " + e.getCliente().getNombre() + " " + e.getCliente().getAppaterno()));
        PdfPCell cel2 = new PdfPCell(new Paragraph("Fecha registro: "  + formato.format(e.getFechareg())));
        cel1.setBorder(Rectangle.NO_BORDER);
        cel2.setBorder(Rectangle.NO_BORDER);
        table3.addCell(cel1);
        table3.addCell(cel2);
        cel1 = new PdfPCell(new Paragraph("Ciudad origen: " + user.getAeropuerto().getCiudad().getDescripcion()));
        cel2 = new PdfPCell(new Paragraph("Servicio: " + dao.obtenerServicio(dao.ObtenerCostoUnitario(user, destino))));
        cel1.setBorder(Rectangle.NO_BORDER);
        cel2.setBorder(Rectangle.NO_BORDER);
        table3.addCell(cel1);
        table3.addCell(cel2);
        cel1 = new PdfPCell(new Paragraph("Ciudad destino: " + destino.getDescripcion()));
        cel2 = new PdfPCell(new Paragraph("Moneda: DOLAR"));
        cel1.setBorder(Rectangle.NO_BORDER);
        cel2.setBorder(Rectangle.NO_BORDER);
        table3.addCell(cel1);
        table3.addCell(cel2);
        parrafo.add(table3);
        
        agregarLineasEnBlanco(ParrafoHoja, 1);
        parrafo = new Paragraph("");
        parrafo.setAlignment(Element.ALIGN_LEFT);
        ParrafoHoja.add(parrafo);

        
        PdfPTable table = new PdfPTable(4); // 4 columns.
        float[] columnWidths = {4f, 1f, 1.5f, 1.5f};
        table.setWidths(columnWidths);
        
        PdfPCell cell1 = new PdfPCell(new Paragraph("Concepto"));
        PdfPCell cell2 = new PdfPCell(new Paragraph("Precio Unitario"));
        PdfPCell cell3 = new PdfPCell(new Paragraph("Cantidad"));
        PdfPCell cell4 = new PdfPCell(new Paragraph("Importe ($)"));
        cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell3.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell4.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);

        cell1 = new PdfPCell(new Paragraph("Envío de Paquetes\n\n\n\n\n"));
        double IGV = dao.ObtenerIGV(user);
        IGV =Utilitario.truncate(IGV);
        double subTotal = Utilitario.truncate(p.getMonto()/(1+IGV));
        double precioU = Utilitario.truncate(subTotal/e.getNumpack());        
        cell2 = new PdfPCell(new Paragraph(String.valueOf(precioU)));
        cell3 = new PdfPCell(new Paragraph(String.valueOf(e.getNumpack())));
        cell4 = new PdfPCell(new Paragraph(String.valueOf(subTotal)));
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);

        cell1 = new PdfPCell(new Paragraph(""));        
        cell2 = new PdfPCell(new Paragraph(""));
        cell3 = new PdfPCell(new Paragraph("Sub total"));
        cell4 = new PdfPCell(new Paragraph(String.valueOf(subTotal)));
        cell1.setBorder(Rectangle.NO_BORDER);
        cell2.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);

        cell1 = new PdfPCell(new Paragraph(""));        
        cell2 = new PdfPCell(new Paragraph(""));
        

        cell3 = new PdfPCell(new Paragraph("IGV ("+String.valueOf(igv) +")")); //Meter el puto porcentaje adentro
        cell4 = new PdfPCell(new Paragraph(String.valueOf(Utilitario.truncate(p.getMonto()- subTotal))));
        cell1.setBorder(Rectangle.NO_BORDER);
        cell2.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);

        cell1 = new PdfPCell(new Paragraph(""));        
        cell2 = new PdfPCell(new Paragraph(""));
        cell3 = new PdfPCell(new Paragraph("TOTAL"));
        cell4 = new PdfPCell(new Paragraph(String.valueOf(p.getMonto())));
        cell1.setBorder(Rectangle.NO_BORDER);
        cell2.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        parrafo.add(table);        

        

        agregarLineasEnBlanco(ParrafoHoja, 2);

        parrafo = new Paragraph("El itinerario del envio es el siguiente:");
        parrafo.setAlignment(Element.ALIGN_LEFT);
        ParrafoHoja.add(parrafo);
        
        PdfPTable table2 = new PdfPTable(4);
        float[] columnWidths2 = {2f, 2f, 2f, 2f};
        table2.setWidths(columnWidths2);
        cell1 = new PdfPCell(new Paragraph("Ciudad Origen"));
        cell2 = new PdfPCell(new Paragraph("Hora Salida"));
        cell3 = new PdfPCell(new Paragraph("Ciudad Destino"));
        cell4 = new PdfPCell(new Paragraph("Hora Llegada"));
        cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell3.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell4.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table2.addCell(cell1);
        table2.addCell(cell2);
        table2.addCell(cell3);
        table2.addCell(cell4);
        
        for (pe.edu.pucp.sigepa.recocidosimulado.Modelo.Vuelo v: lstVuelos){
            
            cell1 = new PdfPCell(new Paragraph(v.getAeropuertoOrigen().getCiudad().getDescripcion()));
            cell2 = new PdfPCell(new Paragraph(formato.format(v.getSalidaest())));
            cell3 = new PdfPCell(new Paragraph(v.getAeropuertoDestino().getCiudad().getDescripcion()));
            cell4 = new PdfPCell(new Paragraph(formato.format(v.getLlegadaest())));            
            table2.addCell(cell1);
            table2.addCell(cell2);
            table2.addCell(cell3);
            table2.addCell(cell4);

                
            }
        parrafo.add(table2); 

        d.add(ParrafoHoja);

   }
   
   private static void agregarLineasEnBlanco(Paragraph parrafo, int nLineas)

    {
        for (int i = 0; i < nLineas; i++)
            parrafo.add(new Paragraph(" "));
    }

//    public static void main(String[] args) {
//    
//        PdfManager p = new PdfManager();
//        Usuario user = new Usuario();
//        user.setNombre("katy linda");
//        user.setApmaterno("tucto");
//        user.setAppaterno("sdadf");
//        user.setDocumento("DNI");
//        Pago pago = new Pago();
//        pago.setFechareg(new Date());
//        pago.setMonto(123.43);
//        pago.setIdpago(1);
//        pago.setUsuario(user);
//        try {
//            p.crearPDFDocumentoPago("/home/katy/testsigepa.pdf", pago, user);
//        } catch (Exception ex) {
//            Logger.getLogger(PdfManager.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
}
